package com.itcast.com.itcast;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itcast.constant.MessageConstant;
import com.itcast.constant.RedisMessageConstant;
import com.itcast.entity.Result;
import com.itcast.service.OrderServcie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Reference
    private OrderServcie orderServcie;

    @Autowired
    private JedisPool jedisPool;
    @RequestMapping("/submit")
    public Result submit(@RequestBody Map map){
        Result result = null;
        if (map==null&&map.size()==0) {
            return new Result(false, MessageConstant.ORDER_FAIL);
        }
            String validateCode = (String) map.get("validateCode");
            String validateCodeRedies = jedisPool.getResource().get((String) map.get("telephone") + RedisMessageConstant.SENDTYPE_ORDER);
            if (validateCode != null && validateCodeRedies != null && validateCode.equalsIgnoreCase(validateCodeRedies)) {
                try {
                    result = orderServcie.submit(map);
                    return result;
                } catch (Exception e) {
                    e.printStackTrace();
                    return new Result(false, MessageConstant.ORDER_FAIL);
                }
            } else {
                return new Result(false, MessageConstant.ORDER_FAIL);
            }


    }


    @RequestMapping("/findById")
    public Result findById(int id){
        Map map= orderServcie.findById(id);
        return new Result(true,MessageConstant.ORDER_SUCCESS,map);
    }
}
